package pers.xiaoliang.concurrency.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import pers.xiaoliang.concurrency.filter.HttpFilter;
import pers.xiaoliang.concurrency.interceptor.HttpInterceptor;

import javax.servlet.Filter;

/**
* @description: 启动器
* @author: xiaoliang.liu
* @date: 2018/7/27 21:17
* @version v1.0
*/
@ComponentScan("pers.xiaoliang.concurrency")
@SpringBootApplication
public class ConcurrencyApplication extends WebMvcConfigurationSupport {

    /**
     * 项目主启动方法
     * @param args 启动时传入参数
     */
    public static void main(String[] args) {
        SpringApplication.run(ConcurrencyApplication.class, args);
    }

    /**
     * 配置自定义的过滤器(注册过滤器)
     * @return FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean httpFilter() {
        FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();

        // 将自定义的过滤器注册上去
        filterFilterRegistrationBean.setFilter(new HttpFilter());

        // 设置过滤的url
        filterFilterRegistrationBean.addUrlPatterns("/thread/*");

        return filterFilterRegistrationBean;
    }

    /**
     * 注册拦截器
     * @param registry 注册机
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HttpInterceptor()).addPathPatterns("/*");
    }
}
